package com.powerunion.datacollection.persistence.pagination;


public class PageHelper {
  public static int pageCount(int pageSize, int total) {
    int a = total % pageSize;
    if (a > 0) {
      return total / pageSize + 1;
    }
    else {
      return total / pageSize;
    }
  }

  public static int prePage(int curPage) {
    if (curPage <= 1) {
      return 1;
    }
    else {
      return curPage - 1;
    }
  }

  public static int nextPage(int curPage, int pageAmount) {
    if (curPage >= pageAmount) {
      return pageAmount;
    }
    else {
      return curPage + 1;
    }
  }

  public static int curPos(int curPage, int pageSize, int total) {
    int i = (curPage - 1) * pageSize;
    if (i >= total) {
      return lastPos(pageSize, total);
    }
    else {
      return i;
    }
  }

  public static int pagePos(int curPos, int pageSize, int total) {
    if (curPos == 0) {
      return 1;
    }
    if (curPos == lastPos(pageSize, total)) {
      return pageCount(pageSize, total);
    }
    return curPos / pageSize + 1;
  }

  public static int nextPos(int curPos, int pageSize, int total) {
    int a = curPos + pageSize;
    if (a < total) {
      return a;
    }
    else {
      return curPos;
    }
  }

  public static int prePos(int curPos, int pageSize) {
    int a = curPos - pageSize;
    if (a >= 0) {
      return a;
    }
    else {
      return curPos;
    }
  }

  public static int lastPos(int pageSize, int total) {
    int a = pageCount(pageSize, total);
    // int b = a * pageSize;
    return (a - 1) * pageSize;
  }

  public static void countPageInfo(PageParameter pInfo, PageResult pr) {
    if (pr.getRecordAmount() > 0) {
      pr.setPageAmount(PageHelper.pageCount(pInfo.getPageSize(),
                                            pr.getRecordAmount()));
      pr.setNextPageNumber(PageHelper.nextPage(pInfo.getPageNumber(),
                                               pr.getPageAmount()));
      pr.setPrePageNumber(PageHelper.prePage(pInfo.getPageNumber()));
      pr.setCurPos(PageHelper.curPos(pInfo.getPageNumber(), pInfo.getPageSize(),
                                     pr.getRecordAmount()));
    }
  }
}
